/**
 * @FilePath: \ros2\src\tools\shared_parameters\src\include\shared_parameters\shared_memory_interface.hpp
 * @Description:  
 * @Author: haibo
 * @Version: 1.0.0
 * @LastEditors: haibo ashengzi@163.com
 * @LastEditTime: 2025-02-20 20:38:55
 * @Copyright (c) 2025 by 临工智能信息科技有限公司, All Rights Reserved. 
**/

#pragma once
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>

namespace bip = boost::interprocess;

// 共享内存配置
struct SharedMemoryConfig {
    static constexpr const char* SHM_NAME = "ros2_params_shm";  // 共享内存名称
    static constexpr const char* MUTEX_NAME = "ros2_params_mutex";  // 互斥锁名称
    static constexpr size_t SHM_SIZE = 10485760;  // 共享内存大小 10x1M
};

// 共享内存中的数据结构
struct SharedData {
    using CharAllocator = bip::allocator<char, bip::managed_shared_memory::segment_manager>;
    using SharedString = bip::basic_string<char, std::char_traits<char>, CharAllocator>;
    
    struct Parameter {
        enum class Type { INT, DOUBLE, STRING, BOOL } type;
        union Value {
            int64_t i;
            double d;
            bool b;
            Value() : i(0) {}
        } value;
        SharedString str_value;  // 用于字符串类型

        Parameter(const CharAllocator& alloc) : str_value(alloc) {}
    };

    using ParamPair = std::pair<const SharedString, Parameter>;
    using ParamAllocator = bip::allocator<ParamPair, bip::managed_shared_memory::segment_manager>;
    using ParamMap = bip::map<SharedString, Parameter, std::less<SharedString>, ParamAllocator>;
};